package com.audaque.springboot.foshanupload.web.screwdemo.controller;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.mysql.cj.jdbc.MysqlDataSource;
import com.zaxxer.hikari.HikariDataSource;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.ArrayList;

/**
 * @author zhongguobin
 * @date 2024-04-03 15:59
 * @description TODO
 */
@RestController

@RequestMapping("screw")
//@FeignClient((contextId = "com.audaque.springboot.foshanupload.web.mbdemo.mpgt.api.SysUserMpgtApi", name = "foshanupload-mbdemo-mpgt",url="",contextId = "sysUserMpgtApi")
@Tag(name = "screw", description = "数据源信息")
public class ScrewController {

    @Value("${dbName}")
    private String dbName;
    @Value("${dbDriverClassName}")
    private String dbDriverClassName;
    @Value("${dbUrl}")
    private String dbUrl;
    @Value("${dbUsername}")
    private String dbUsername;
    @Value("${dbPassword}")
    private String dbPassword;
    @Value("${dbType}")
    private String dbType;
    @Value("${dbDataSourceClass}")
    private String dbDataSourceClass;


    @Operation(summary = "生成")
    @RequestMapping(value = "/generate", method = {RequestMethod.GET})
    @ResponseBody
    public void generate() throws Exception {
        DataSource dataSourceMysql = DataSourceBuilder.create()
                .driverClassName(dbDriverClassName)
                .password(dbPassword)
                .type(HikariDataSource.class)
                .url(dbUrl)
                .username(dbUsername)
                .build();

        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径，自己mac本地的地址，这里需要自己更换下路径
                .fileOutputDir("./generated/screw/")
                // 打开目录
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.WORD)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文档配置（包含以下自定义版本号、描述等配置连接）
        Configuration config = Configuration.builder()
                .version("1.0.0")
                .description(dbName)
                .dataSource(dataSourceMysql)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();

        // 执行生成
        new DocumentationExecute(config).execute();
    }


    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        // 忽略表名
        //List<String> ignoreTableName = Arrays.asList("aa", "test_group");
        // 忽略表前缀，如忽略a开头的数据库表
        // List<String> ignorePrefix = Arrays.asList("a", "t");
        // 忽略表后缀
        //List<String> ignoreSuffix = Arrays.asList("_test", "czb_");

        return ProcessConfig.builder()
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                //.ignoreTableName(ignoreTableName)
                //忽略表前缀
                //.ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                //.ignoreTableSuffix(ignoreSuffix)
                .build();
    }
}
